package com.zhxy.util.mybatis.plugin;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class ColumnDefaultValuePlugin extends PluginAdapter {
	
	private HashMap<String, String> datas = new HashMap<String, String>();
	
	private Field contentByTextElement;
	
	private Field elementsByXmlElement;

	@Override
	public boolean validate(List<String> warnings) {
		
		boolean isValid = true;
		
		for (Entry<Object, Object> entry : properties.entrySet()) {
			datas.put(entry.getKey().toString(), entry.getValue().toString());
		}
		
		try {
			contentByTextElement = TextElement.class.getDeclaredField("content");
			contentByTextElement.setAccessible(true);
			elementsByXmlElement = XmlElement.class.getDeclaredField("elements");
			elementsByXmlElement.setAccessible(true);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return isValid;
	}


	@Override
	public boolean sqlMapInsertElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		
		String content = "";
		for (Element el : element.getElements()) {
			content += el.getFormattedContent(0) + "\n";
		}
		
		String c = replace(content);
		
		
		ArrayList<Element> list = new ArrayList<Element>();
		list.add(new TextElement(c));
		try {
			elementsByXmlElement.set(element, list);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return true;
	}
	
	@Override
	public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element,
			IntrospectedTable introspectedTable) {
		
		String content = "";
		for (Element el : element.getElements()) {
			content += el.getFormattedContent(0) + "\n";
		}
		
		String c = replaceSelective(content);
		
		ArrayList<Element> list = new ArrayList<Element>();
		list.add(new TextElement(c));
		try {
			elementsByXmlElement.set(element, list);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return true;
	}


	private String replace(String content) {
		
		for (Entry<String, String> entry : datas.entrySet()) {
			
			Pattern p = Pattern.compile("#\\{" + entry.getKey() + ".*?\\}");
			
			Matcher m = p.matcher(content);
			if (m.find()) {
				content = content.substring(0, m.start()) + entry.getValue() + content.substring(m.end());
			}
		}
		return content;
	}
	
	private String replaceSelective(String content) {
		
		for (Entry<String, String> entry : datas.entrySet()) {
			
			content = content.replaceAll(entry.getKey() + " != null", "1 == 1");
			System.out.println(content);
			
			Pattern p = Pattern.compile("#\\{" + entry.getKey() + ".*?\\}");
			Matcher m = p.matcher(content);
			if (m.find()) {
				content = content.substring(0, m.start()) + entry.getValue() + content.substring(m.end());
			}
		}
		return content;
	}
	
	public static void main(String[] args) {
		
		ColumnDefaultValuePlugin p = new ColumnDefaultValuePlugin();
		p.datas = new HashMap<String, String>();
		p.datas.put("id", "now()");
		
		p.replace("values (#{id,jdbcType=CHAR}, #{createdTime,jdbcType=TIMESTAMP}, #{updatedTime,jdbcType=TIMESTAM");
	}

}
